//
//  PoiSearchViewController.m
//  wtshoppingcite
//
//  Created by TW on 15-3-19.
//  Copyright (c) 2015年 TW. All rights reserved.
//

#import "PoiSearchViewController.h"
#import "PoiSearchTableView.h"
#import "PoiSearchDetailViewController.h"
#import "AppDelegate.h"

@interface PoiSearchViewController ()<UITableViewEventsDelegate,BMKGeoCodeSearchDelegate,BMKPoiSearchDelegate>
{
    BOOL _isMore;
}

@property(strong,nonatomic)BMKPoiSearch *poisearch;
@property(strong,nonatomic)BMKGeoCodeSearch*geocodesearch;
@property(strong,nonatomic)PoiSearchTableView *poiSearchTableView;
@property(strong,nonatomic)BMKNearbySearchOption *citySearchOption;

@end

@implementation PoiSearchViewController

- (void)viewDidLoad {
    [super viewDidLoad];
    [self setTitleView:_keyText];
    _dataArray = [[NSMutableArray alloc]initWithCapacity:1];
    _poiSearchTableView = [[PoiSearchTableView alloc]initWithFrame:CGRectMake(0, 0, SCREEN_WIDTH, SCREEN_HEIGHT-44) style:UITableViewStylePlain];
    _poiSearchTableView.isNeedTorefresh = YES;
    _poiSearchTableView.index = 0;
    _poiSearchTableView.eventsDelegate = self;
    _poiSearchTableView.isMore = YES;
    _poiSearchTableView.userInteractionEnabled = YES;
    _isMore = NO;
    [self.view addSubview:_poiSearchTableView];
    _geocodesearch = [[BMKGeoCodeSearch alloc]init];
    _poisearch = [[BMKPoiSearch alloc]init];
    
    BMKUserLocation *userLocation = [AppDelegate sharedDelegate].userlocation;
    [self showProgressHUD];
    CLLocationCoordinate2D pt = (CLLocationCoordinate2D){0, 0};
    if (userLocation) {
        pt = userLocation.location.coordinate;
    }
    BMKReverseGeoCodeOption *reverseGeocodeSearchOption = [[BMKReverseGeoCodeOption alloc]init];
    reverseGeocodeSearchOption.reverseGeoPoint = pt;
    BOOL flag = [_geocodesearch reverseGeoCode:reverseGeocodeSearchOption];
    if(flag)
    {
        NSLog(@"反geo检索发送成功");
    }
    else
    {
        NSLog(@"反geo检索发送失败");
        [self hideProgressHUD];
    }
}



- (void)viewWillAppear:(BOOL)animated{
    [super viewWillAppear:animated];
    _geocodesearch.delegate = self;
    _poisearch.delegate = self;
}

- (void)viewWillDisappear:(BOOL)animated{
    [super viewWillDisappear:animated];
    _geocodesearch.delegate = nil;
    _poisearch.delegate = nil;
}


#pragma mark - BMKGeoCodeSearchDelegate
-(void)onGetReverseGeoCodeResult:(BMKGeoCodeSearch *)searcher result:(BMKReverseGeoCodeResult *)result errorCode:(BMKSearchErrorCode)error
{
    if (error == 0) {
        _cityText = result.addressDetail.city;
        _curPage = 0;
        _citySearchOption = [[BMKNearbySearchOption alloc]init];
        _citySearchOption.pageIndex = _curPage;
        _citySearchOption.pageCapacity = 10;
        _citySearchOption.location= result.location;
        _citySearchOption.keyword = _keyText;
        _citySearchOption.radius = 1000;
        BOOL flag = [_poisearch poiSearchNearBy:_citySearchOption];
        if(flag)
        {
            NSLog(@"城市内检索发送成功");
        }
        else
        {
            NSLog(@"城市内检索发送失败");
        }

    }
    else{
        [self showToastMessage:@"地址解析失败"];
    }
}

-(void)getPoiPageData{

    [self showProgressHUD];
    _citySearchOption.pageIndex = _curPage;
    _citySearchOption.pageCapacity = 10;
    BMKUserLocation *userLocation = [AppDelegate sharedDelegate].userlocation;
    _citySearchOption.location = userLocation.location.coordinate;
    _citySearchOption.keyword = _keyText;
    _citySearchOption.radius = 1000;
    BOOL flag = [_poisearch poiSearchNearBy:_citySearchOption];
    if(flag)
    {
        NSLog(@"城市内检索发送成功");
    }
    else
    {
        NSLog(@"城市内检索发送失败");
    }
    [_poiSearchTableView doneLoadingTableViewData];
    [_poiSearchTableView reloadData];
}

#pragma mark  eventsDelegate
- (void)pullUp:(BaseTableView *)baseTableView{
    
    _isMore = YES;
    [self getPoiPageData];
}

- (void)pullDown:(BaseTableView *)baseTableView{
    _curPage = 0;
    _isMore = NO;
    [self getPoiPageData];
}


//返回POI搜索结果
#pragma mark implement BMKSearchDelegate
- (void)onGetPoiResult:(BMKPoiSearch *)searcher result:(BMKPoiResult*)result errorCode:(BMKSearchErrorCode)error
{
    NSLog(@"%@",result);
    [self hideProgressHUD];
    if (error == BMK_SEARCH_NO_ERROR) {
        
        if (result.poiInfoList.count < 10) {
            _poiSearchTableView.isMore = NO;
        }else{
            _poiSearchTableView.isMore = YES;
            
            _curPage++;
            
        }
        if (!_isMore) {
            [_dataArray removeAllObjects];
        }
        for (int i = 0; i < result.poiInfoList.count; i++) {
            
            BMKPoiInfo* poi = [result.poiInfoList objectAtIndex:i];
           
            [_dataArray addObject:poi];
            
        }
        _poiSearchTableView.data = _dataArray;
        [_poiSearchTableView doneLoadingTableViewData];
        [_poiSearchTableView reloadData];
        
    } else if (error == BMK_SEARCH_AMBIGUOUS_ROURE_ADDR){
        NSLog(@"起始点有歧义");
    } else {
        // 各种情况的判断。。。
    }
}

- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

-(void)basetableView:(BaseTableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath{
    
    BMKPoiInfo *poiInfo = self.dataArray[indexPath.row];
    PoiSearchDetailViewController *poiSearchDetailVC = [[PoiSearchDetailViewController alloc]init];
    poiSearchDetailVC.hidesBottomBarWhenPushed = YES;
    poiSearchDetailVC.uid = poiInfo.uid;
    [self.navigationController pushViewController:poiSearchDetailVC animated:YES];
    
}



@end
